package com.midele.goodstuff.viewmodel

import androidx.lifecycle.viewModelScope
import com.midele.goodstuff.comm.ComposeBaseViewModel
import com.midele.goodstuff.ui.main.MainActivityIntent
import com.midele.goodstuff.ui.main.MainActivityState
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class MainViewModel @Inject constructor() :
    ComposeBaseViewModel<MainActivityState, MainActivityIntent>(
        MainActivityState()
    ) {

    override fun handleEvent(
        event: MainActivityIntent,
        state: MainActivityState
    ) {
        when (event) {
            is MainActivityIntent.SelectNavItem -> selectNavItem(event.index)
            is MainActivityIntent.SetShowBottomBar -> {
                viewStates.update { copy(isShowBottomBar = event.state) }
            }
        }
    }

    private fun selectNavItem(index: Int) {
        viewStates = viewStates.copy(titleState = false)
        viewModelScope.launch {
            delay(250L)
            viewStates.update {
                copy(titleState = true)
            }
        }
        viewStates.update {
            copy(navItemIndex = index)
        }
    }
}